package org.example.coffeeshoporderingsystem.store.controller;

import javafx.beans.InvalidationListener;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.geometry.Insets;
import javafx.scene.control.*;
import javafx.scene.control.Button;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.HBox;
import javafx.scene.paint.Color;
import org.example.coffeeshoporderingsystem.common.controller.BaseController;
import org.example.coffeeshoporderingsystem.common.util.SpringContextUtil;
import org.example.coffeeshoporderingsystem.product.entity.Product;
import org.example.coffeeshoporderingsystem.product.service.ProductService;
import org.example.coffeeshoporderingsystem.store.entity.Store;
import org.example.coffeeshoporderingsystem.store.service.StoreService;



import java.net.URL;
import java.util.ResourceBundle;

import static javafx.scene.paint.Color.BLACK;
import static javafx.scene.paint.Color.WHITE;

public class StoreController extends BaseController {
    // 中央显示 Panel
    public AnchorPane centerPanel;
    // 门店信息面板
    public AnchorPane storePanel;
    // 菜单信息面板
    public AnchorPane productPanel;
    // 添加商品对话框
    public DialogPane addProductDialogPane;
    // 门店信息
    public TableView<Store> storeTableView;
    public TableColumn<Store,Integer> storeIdTabCol;
    public TableColumn<Store,String> storeNameTabCol;
    public TableColumn<Store,String> storeAddressTabCol;
    public TableColumn<Store,String> storePhoneNumberTabCol;
    public TableColumn<Store,String> storeOpeningHoursTabCol;
    public TableColumn<Store,String> storeManagerNameTabCol;
    public TableColumn<Store, HBox> storeOpeTabCol;

    // 导入Excel对话框
    public DialogPane importExcelDialogPane;
    public Button loadStoreBtn;
    public Button loadProductBtn;
    public Button loadOrderBtn;


    private StoreService storeService = SpringContextUtil.getBean(StoreService.class) ;
    private ObservableList<Store> storeObservableList = FXCollections.observableArrayList() ;


    // 菜单信息
    public TableView<Product> productTableView;
    public TableColumn<Product,Integer> productIdTabCol;
    public TableColumn<Product,String> productNameTabCol;
    public TableColumn<Product,String> categoryNameTabCol;
    public TableColumn<Product,String> priceTabCol;
    public TableColumn<Product,String> tagsTabCol;
    public TableColumn<Product, CheckBox> isFreeTabCol;
    public TableColumn<Product, HBox> productOpeTabCol;
    private ProductService productService = SpringContextUtil.getBean(ProductService.class) ;
    private ObservableList<Product> productObservableList = FXCollections.observableArrayList() ;
    @Override
    public void initialize(URL url, ResourceBundle resourceBundle) {
        // 绑定数据 门店信息
        storeTableView.setItems(storeObservableList);
        // 绑定表格数据
        storeIdTabCol.setCellValueFactory(new PropertyValueFactory<>("storeId"));
        storeNameTabCol.setCellValueFactory(new PropertyValueFactory<>("name"));
        storeAddressTabCol.setCellValueFactory(new PropertyValueFactory<>("address"));
        storePhoneNumberTabCol.setCellValueFactory(new PropertyValueFactory<>("phoneNumber"));
        storeOpeningHoursTabCol.setCellValueFactory(new PropertyValueFactory<>("openingHours"));
        storeManagerNameTabCol.setCellValueFactory(cell->new SimpleStringProperty(cell.getValue().getManager().getName()));
        // 加载门店信息
        this.loadStore();

        // 绑定表格数据 菜单信息
        productTableView.setItems(productObservableList);
        // 绑定表格数据
        productIdTabCol.setCellValueFactory(new PropertyValueFactory<>("productId"));
        productNameTabCol.setCellValueFactory(new PropertyValueFactory<>("name"));
        categoryNameTabCol.setCellValueFactory(cell->new SimpleStringProperty(cell.getValue().getCategory().getName()));
        priceTabCol.setCellValueFactory(new PropertyValueFactory<>("price"));
        //tagsTabCol.setCellValueFactory(new PropertyValueFactory<>("openingHours"));
        isFreeTabCol.setCellValueFactory(cell->new ObjectProperty<CheckBox>() {

            @Override
            public void addListener(InvalidationListener invalidationListener) {

            }

            @Override
            public void removeListener(InvalidationListener invalidationListener) {

            }

            @Override
            public void addListener(ChangeListener<? super CheckBox> changeListener) {

            }

            @Override
            public void removeListener(ChangeListener<? super CheckBox> changeListener) {

            }

            @Override
            public CheckBox get() {
                var ck = new CheckBox() ;
                ck.setSelected(true);
                return ck ;
            }

            @Override
            public void set(CheckBox checkBox) {

            }

            @Override
            public Object getBean() {
                return null;
            }

            @Override
            public String getName() {
                return "";
            }

            @Override
            public void bind(ObservableValue<? extends CheckBox> observableValue) {

            }

            @Override
            public void unbind() {

            }

            @Override
            public boolean isBound() {
                return false;
            }
        });
        productOpeTabCol.setCellValueFactory(cell->new ObjectProperty<HBox>() {
            @Override
            public void bind(ObservableValue<? extends HBox> observableValue) {

            }

            @Override
            public void unbind() {

            }

            @Override
            public boolean isBound() {
                return false;
            }

            @Override
            public Object getBean() {
                return null;
            }

            @Override
            public String getName() {
                return "";
            }

            @Override
            public HBox get() {
                var hbox = new HBox() ;
                var editImage = new ImageView(new Image("https://demo-video-oss.oss-cn-hangzhou.aliyuncs.com/coffee/edit.png"));
                var delImage =  new ImageView(new Image("https://demo-video-oss.oss-cn-hangzhou.aliyuncs.com/coffee/del-product.png"));
                var viewLabel = new Label("查看");
                // 设置背景色为 蓝色
                viewLabel.setStyle("-fx-background-color: #ff9800;");
                // 设置字体为白色
                viewLabel.setTextFill(Color.WHITE);

                // 设置hbox 边距 10px
                hbox.setSpacing(10);
                // 设置hbox 组件内边距
                hbox.setPadding(new Insets(10));
                hbox.getChildren().addAll(editImage,delImage,viewLabel) ;
                return hbox;
            }

            @Override
            public void addListener(ChangeListener<? super HBox> changeListener) {

            }

            @Override
            public void removeListener(ChangeListener<? super HBox> changeListener) {

            }

            @Override
            public void addListener(InvalidationListener invalidationListener) {

            }

            @Override
            public void removeListener(InvalidationListener invalidationListener) {

            }

            @Override
            public void set(HBox hBox) {

            }
        });
    }

    public void loadStore() {
        storeObservableList.clear();
        storeObservableList.addAll(storeService.findAll());
        centerPanel.getChildren().clear();
        centerPanel.getChildren().add(storePanel);

        loadStoreBtn.setStyle("-fx-background-color: green;");
        loadStoreBtn.setTextFill(WHITE);

        loadProductBtn.setStyle("-fx-background-color: #fff;");
        loadProductBtn.setTextFill(BLACK);

        loadOrderBtn.setStyle("-fx-background-color: #fff;");
        loadOrderBtn.setTextFill(BLACK);
    }

    public void loadProduct() {
        productObservableList.clear();
        productObservableList.addAll(productService.findAll());
        centerPanel.getChildren().clear();
        centerPanel.getChildren().add(productPanel);
        loadProductBtn.setStyle("-fx-background-color: green;");
        loadProductBtn.setTextFill(WHITE);

        loadStoreBtn.setStyle("-fx-background-color: #fff;");
        loadStoreBtn.setTextFill(BLACK);

        loadOrderBtn.setStyle("-fx-background-color: #fff;");
        loadOrderBtn.setTextFill(BLACK);

    }

    public void showAddProductDialogPane() {
        // 显示对话框
        addProductDialogPane.setVisible(true);
    }

    public void saveAddProduct() {
        // 关闭对话框
        addProductDialogPane.setVisible(false);
    }

    public void closeAddProductDialogPane() {
        addProductDialogPane.setVisible(false);
    }

    public void showImportExcelDialogPane() {
        importExcelDialogPane.setVisible(true);
    }

    public void confirmImportProduct() {
        importExcelDialogPane.setVisible(false);
    }
}
